<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS-Interpreter Demo</title>
  <link href="demos/style.css" rel="stylesheet" type="text/css">

  <script>
    // Depending on the URL argument, load the compressed or uncompressed version.
    var compressed = (document.location.search === '?compressed');
    if (compressed) {
      document.write('<scr'+'ipt src="acorn_interpreter.js"></scr'+'ipt>');
    } else {
      document.write('<scr'+'ipt src="acorn.js"></scr'+'ipt>');
      document.write('<scr'+'ipt src="interpreter.js"></scr'+'ipt>');
    }

    var myInterpreter;
    function initAlert(interpreter, globalObject) {
      var wrapper = function alert(text) {
        return window.alert(arguments.length ? text : '');
      };
      interpreter.setProperty(globalObject, 'alert',
          interpreter.createNativeFunction(wrapper));
    }

    function parseButton() {
      var code = document.getElementById('code').value;
      myInterpreter = new Interpreter(code, initAlert);
      disable('');
    }

    function stepButton() {
      var stack = myInterpreter.getStateStack();
      if (stack.length) {
        var node = stack[stack.length - 1].node;
        var start = node.start;
        var end = node.end;
      } else {
        var start = 0;
        var end = 0;
      }
      createSelection(start, end);
      try {
        var ok = myInterpreter.step();
      } finally {
        if (!ok) {
          disable('disabled');
        }
      }
    }

    function runButton() {
      disable('disabled');
      if (myInterpreter.run()) {
        // Async function hit.  There's more code to run.
        setTimeout(runButton, 100);
      }
    }

    function disable(disabled) {
      document.getElementById('stepButton').disabled = disabled;
      document.getElementById('runButton').disabled = disabled;
    }

    function createSelection(start, end) {
      var field = document.getElementById('code');
      if (field.createTextRange) {
        var selRange = field.createTextRange();
        selRange.collapse(true);
        selRange.moveStart('character', start);
        selRange.moveEnd('character', end);
        selRange.select();
      } else if (field.setSelectionRange) {
        field.setSelectionRange(start, end);
      } else if (field.selectionStart) {
        field.selectionStart = start;
        field.selectionEnd = end;
      }
      field.focus();
    }
  </script>
</head>
<body>
  <h1>JS-Interpreter Demo</h1>

  <p>Enter JavaScript code below, then click <em>Parse</em>.  To execute, either
  click <em>Step</em> repeatedly, or click <em>Run</em> once.
  Open your browser's console for errors.</p>
  <p><textarea id="code" spellcheck="false">
var result = [];
function fibonacci(n, output) {
  var a = 1, b = 1, sum;
  for (var i = 0; i &lt; n; i++) {
    output.push(a);
    sum = a + b;
    a = b;
    b = sum;
  }
}
fibonacci(16, result);
alert(result.join(', '));
</textarea><br>
  <button onclick="parseButton()">Parse</button>
  <button onclick="stepButton()" id="stepButton" disabled="disabled">Step</button>
  <button onclick="runButton()" id="runButton" disabled="disabled">Run</button>
  </p>

  <p><small>
  <script>
    if (compressed) {
      document.write('[ Running compressed bundle.  Switch to <A HREF="?uncompressed">raw sources</A>. ]');
    } else {
      document.write('[ Running raw sources.  Switch to <A HREF="?compressed">compressed bundle</A>. ]');
    }
    disable('disabled');
  </script>
  </small></p>

  <p>Read the <a href="docs.html">JS-Interpreter documentation</a>.</p>
  <p>Get the <a href="https://github.com/NeilFraser/JS-Interpreter">source code</a>.</p>

</body>
</html>
